package com.artron.mediaartron.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import com.artron.baselib.image.ImageUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.FrameData;
import com.artron.mediaartron.ui.fragment.dialog.NormalDialogFragment;
import com.artron.mediaartron.ui.fragment.made.edit.EditPageFragment;
import com.artron.mediaartron.ui.linkpage.EditLinkPage;
import com.artron.mediaartron.util.SharePreferenceUtils;
import com.bilibili.boxing.Boxing;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action0;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-28.
 */

public class EditPageActivity extends AppBaseActivity {

    private static final String TAG = "EditPageActivity";

    static final String EXTRA_RESULT = "com.bilibili.boxing.Boxing.result";
    public static final String DATA = "data";
    private BaseMedia mBaseMedia;

    public static void start(Context context, FrameData data) {
        Intent starter = new Intent(context, EditPageActivity.class);
        starter.putExtra(DATA, data);
        context.startActivity(starter);
    }

    public static void startForResult(Activity context, FrameData data, int requestCode) {
        Intent starter = new Intent(context, EditPageActivity.class);
        starter.putExtra(DATA, data);
        context.startActivityForResult(starter, requestCode);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        FrameData data = getIntent().getParcelableExtra(DATA);
        EditPageFragment fragment = EditPageFragment.newInstance(data);
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments == null || fragments.size() == 0) {
            addFragment(R.id.General_content,fragment, false);
        } else {
            replaceFragment(R.id.General_content, fragment, false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edit_page, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void initActionBar() {
        mTvTitle.setText("编辑页");
        mToolbar.setNavigationIcon(R.drawable.ic_back);
        super.initActionBar();

        getWindow().setBackgroundDrawableResource(android.R.color.white);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ArrayList<BaseMedia> result = Boxing.getResult(data);
        if (result != null && result.size() > 0) {
            mBaseMedia = result.get(0);
            ((EditPageFragment) mCurrentFragment)
                    .updateImagePath(mBaseMedia.getPath());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        NormalDialogFragment dialog = NormalDialogFragment.newInstance("是否放弃编辑？", "否", "是");
        dialog.setOnNegativeClickListener(new Action0() {
            @Override
            public void call() {
                onBackPressWithImage();
            }
        });
        dialog.show(getSupportFragmentManager(), dialog.getOurTag());
    }

    public void onBackPressWithImage() {
        FrameData data = getIntent().getParcelableExtra(DATA);
        String path = data.getImage().getContentImage();
        List<BaseMedia> medias = new ArrayList<>();
        if (mBaseMedia != null) {
            medias.add(mBaseMedia);
        } else {
            ImageMedia media = new ImageMedia.Builder("idDoing", path).build();
            medias.add(media);
        }
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(EXTRA_RESULT, (ArrayList<BaseMedia>) medias);
        setResult(RESULT_OK, intent);
        EditPageActivity.super.onBackPressed();
    }

    @Override
    protected View initLinkPageView(ViewGroup parent) {
        String preferenceKey = "EditPage";

        boolean isFirst = SharePreferenceUtils.getInstance().getBoolean(preferenceKey, true);
        if (isFirst) {
            SharePreferenceUtils.getInstance().putBoolean(preferenceKey, false);
            return new EditLinkPage(parent).getContentView();
        } else {
            return super.initLinkPageView(parent);
        }
    }

    @Override
    public void onTrimMemory(int level) {
        Log.d(TAG, "onTrimMemory: " + level);
        super.onTrimMemory(level);
    }

    @Override
    public void onLowMemory() {
        Log.d(TAG, "onLowMemory: ");
        ImageUtils.clearMemoryCache(this);
        super.onLowMemory();
    }
}
